/*
 * AppTest.cpp
 *
 *  Created on: 12 Nov 2012
 *      Author: guy
 */

#include <iostream>
#include "core/Scene.h"
#include "core/Application.h"
#include "components/renderers/SpriteRenderer.h"
#include "components/SpriteAnimator.h"
#include "components/renderers/TextRenderer.h"
#include "components/AudioSource.h"
#include "Behaviours.h"
#include "core/Resources.h"
#include "components/colliders/CircleCollider.h"

using namespace SolidityEngine;

Scene* sceneBuild(const std::string& bgimage, int w, int h)
{
    Scene* scene = new Scene("scene",w,h);

    //Set a backdrop image for the scene
    scene->SetBackdropImage(Resources::GetResource<ImageResource>(bgimage));
    GameObject* explosion = new GameObject("boom");
    explosion->Transform()->Position()=Vector2D(Screen::Width()/2.0f,Screen::Height()/2.0f);
    SpriteRenderer* x_ren=explosion->AddComponent<SpriteRenderer>();
    explosion->AddComponent<Explosion>();
    x_ren->SetImage(Resources::GetResource<ImageResource>("RedExplosion"));
    SpriteAnimator* x_animator=explosion->AddComponent<SpriteAnimator>();
    x_animator->SetSpritesheetSize(3,4);
    x_animator->Play(0,x_animator->GetFrameCount());

    GameObject* bullet = new GameObject("bullet");
    bullet->Tag()="bullet";
    SpriteRenderer* b_ren = bullet->AddComponent<SpriteRenderer>();
    b_ren->SetImage(Resources::GetResource<ImageResource>("bullet"));
    bullet->AddComponent<BulletMove>();
    bullet->AddComponent<CircleCollider>();
    AudioSource* a_source = bullet->AddComponent<AudioSource>();
    a_source->SetAudioClip(Resources::GetResource<AudioClip>("Laser"));

    GameObject* player = scene->AddObject("player");
    SpriteRenderer* p_ren = player->AddComponent<SpriteRenderer>();
    p_ren->SetImage(Resources::GetResource<ImageResource>("medspeedster"));
    player->AddComponent<PlayerMove>();
    PlayerGun* p_gun=player->AddComponent<PlayerGun>();
    p_gun->bulletprefab=bullet;

    //Create an object to use as a prefab
    GameObject* enemy=new GameObject("enemy_mine");
    enemy->Tag()="enemy";

    //Add a sprite renderer to this
    SpriteRenderer* e_ren = enemy->AddComponent<SpriteRenderer>();
    //Load an image resource
    e_ren->SetImage(Resources::GetResource<ImageResource>("Sox_64x64"));
    //Add a collider
    enemy->AddComponent<CircleCollider>();
    EnemyMove* e_movr = enemy->AddComponent<EnemyMove>();
    e_movr->explosion=explosion;

    //Create an object in the scene to act as a factory
    GameObject* factory=scene->AddObject("factory_object");
    //Add the factory behaviour component
    EnemyFactory* factory_script=factory->AddComponent<EnemyFactory>();
    //Set the behaviour parameter
    factory_script->enemy=enemy;

    //Create a text object
    GameObject* textobject=scene->AddObject("text");
    //Add a text renderer to this
    TextRenderer* t_ren=textobject->AddComponent<TextRenderer>();
    //ensure it always draws on top
    t_ren->Index()=100;
    //Set the font
    t_ren->SetFont(Resources::GetResource<FontResource>("FreeSans"));
    //Set the text
    t_ren->Label()="Solidity test app.";
    //Set the color
    Color32 col= Color32(255,0,0,255);
    t_ren->Color()=col;

    //set its position
    textobject->GetComponent<Transform2D>()->Position() = Vector2D(Screen::Width()/2.0f,Screen::Height()/2.0f);
    return scene;
}

int main(int argc, char** argv)
{
    //Some stuff to enable console output in SDL

	#ifdef _WIN32
	freopen( "CON", "wt", stdout );
	freopen( "CON", "wt", stderr );
	#endif

    std::cout<<"Solidity Test Player 1.0"<<std::endl;

    //Resources res;
    Application::Init();

    //Set a requested Screen resolution and clear color
    Screen::SetResolution(Screen::Resolution(800,600,false), Color32(0,0,0));

    //Create a new Scene (will change how this is done)
    Scene* scene = sceneBuild("plate4",1600,600);

    //Load our level
    Application::SetScene(scene);

    //Now we just need a frame loop
	Application::Play();

    //Clean up
	delete scene;
	scene=NULL;

	return 0;
}


